<?php
namespace app\redbag\model;

use app\BaseModel;
use think\facade\Db;

class staffRecord extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_redbag_staff_record';


    /**
     * @param $value
     * @param $data
     * @功能说明:显示状态
     * @author chenniang
     * @DataTime: 2020-04-11 18:34
     */
    public function getIsShowAttr($value,$data){


        if(!empty($data['status'])&&$data['status']==-1){

           return 0;

        }
        return $value;
    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:发放记录
     * @author chenniang
     * @DataTime: 2020-04-09 17:30
     */

    public function AdminList($dis,$mapor,$page){


        $data = $this->alias('a')
                ->join('longbing_card_user b','a.staff_id = b.id')
                ->join('longbing_card_user_info c','a.staff_id = c.fans_id')
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })
                ->field('a.*,c.name as staff_name,b.nickName')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();
        if(!empty($data['data'])){

            //记录模型
            $record_model  = new Record();

            foreach ($data['data'] as &$v){

                //使用数量
                $record_dis = [
                   //状态
                  'status'    => 1,
                   //员工
                  'staff_id'  => $v['staff_id'],
                   //活动
                  'active_id' => $v['active_id']
                ];
                $use_count   = $record_model->where($record_dis)->count();
                //剩余数量
                $v['y_num']  = $v['total_num'] - $use_count;

                $v['y_num']  = $v['y_num']>=0?$v['y_num']:0;
                //使用金额
                $use_price   = $record_model->where($record_dis)->sum('price');

                $v['y_price'] = round($v['total_price'] - $use_price,2);

                $v['y_price']  = $v['y_price']>=0?$v['y_price']:0;
            }

        }
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-09 17:53
     * @功能说明:停止发放
     */
   public function stopGive($dis){
       //列表
       $list = $this->where($dis)->select()->toArray();

       if(!empty($list)){

           //记录模型
           $record_model  = new Record();

           foreach ($list as $value){
                //停止发放
               $this->where(['id'=>$value['id']])->update(['status'=>-1,'is_show'=>0]);
                //领取记录也停止
               $record_dis = [
                   //状态
                   'status'    => 0,
                   //员工
                   'staff_id'  => $value['staff_id'],
                   //活动
                   'active_id' => $value['active_id']
               ];

               $record_model->where($record_dis)->update(['status'=>-1]);
           }

       }
       return true;
   }


    /**
     * @author chenniang
     * @DataTime: 2020-04-09 18:21
     * @功能说明:详情
     */
   public function recordInfo($dis){

       $data = $this->where($dis)->find();

       return !empty($data)?$data->toArray():[];

   }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 13:45
     * @功能说明:添加记录
     */
   public function recordAdd($data){

       $data['create_time'] = time();

       $data['update_time'] = time();

       $res = $this->insert($data);

       return $res;
   }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 14:08
     * @功能说明:初始化
     */
   public function initStaffRecord(){

       $res = $this->where([['status','<>',0]])->update(['is_show'=>0]);

       return $res;

   }

    /**
     * @author chenniang
     * @DataTime: 2020-03-23 17:31
     * @功能说明:加数量
     */
    public function recordInset($dis,$filed,$num=1){

        $res  = $this->where($dis)->update(["$filed"=>Db::raw("$filed+$num")]);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-11 10:29
     * @功能说明:员工端列表
     */
    public function staffRecordList($where,$staff_id){


        $data = $this->alias('a')
                ->join('longbing_card_redbag_active b','a.active_id = b.id')
                ->where($where)
                ->field('b.*,a.status as redbag_status,a.is_show')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate(10)
                ->toArray();
        if(!empty($data['data'])){
            //记录模型
            $record_model  = new Record();

            foreach ($data['data'] as &$v){
                //使用数量
                $record_dis = [
                    //状态
                    'status'    => 1,
                    //员工
                    'staff_id'  => $staff_id,
                    //活动
                    'active_id' => $v['id']
                ];
                //使用数量
                $v['use_count'] = $record_model->where($record_dis)->count();
                //转换时间
                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

                $v['is_show']  = $v['redbag_status']==-1?0: $v['is_show'];
            }
        }
        return $data;
    }


    /**
     * @param $data
     * @功能说明:改变状态
     * @author chenniang
     * @DataTime: 2020-04-11 18:59
     */
    public function changeStatus($uniacid){

        $data = $this->where(['status'=>0,'uniacid'=>$uniacid])->select()->toArray();

        if(!empty($data)){

            foreach ($data as $v){

                $dis = [
                    'active_id' => $v['active_id'],

                    'staff_id'  => $v['staff_id'],

                    'status'    => 1

                ];
                //记录模型
                $record = new Record();

                $count  = $record->where($dis)->count();

                if($count>=$v['total_num']){

                    $this->where(['id'=>$v['id']])->update(['status'=>1]);
                }

            }
        }

        return true;
    }


}